#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class ParentCodeProvider : CY.GFive.Core.Providers.DALProvider.IParentCodeProvider
	{
		#region Sql
		private static readonly string SqlSelectParentCode = "SELECT [Id], [PCateCode],[PName],[Des] FROM [ParentCode] WHERE  [Id]=@Id";

		private static readonly string SqlInsertParentCode = "INSERT INTO [ParentCode]([PCateCode],[PName],[Des]) values(@PCateCode,@PName,@Des);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateParentCode = "UPDATE [ParentCode] SET [PCateCode] = @PCateCode,[PName] = @PName,[Des] = @Des WHERE  [Id]=@Id";

		private static readonly string SqlDeleteParentCode = "DELETE [ParentCode] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllParentCode = "SELECT [Id], [PCateCode],[PName],[Des] FROM [ParentCode]";
		#endregion

		#region IParentCodeProvider Members
		public Core.Business.ParentCode Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectParentCode);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.ParentCode parentCode = new Core.Business.ParentCode();

				if (!reader.IsDBNull(0)) parentCode.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) parentCode.PCateCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) parentCode.PName = reader.GetString(2);
				if (!reader.IsDBNull(3)) parentCode.Des = reader.GetString(3);

				return parentCode;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.ParentCode parentCode)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PCateCode", SqlDbType.NVarChar, parentCode.PCateCode);
			sql.AddParameter("@PName", SqlDbType.NVarChar, parentCode.PName);
			sql.AddParameter("@Des", SqlDbType.NVarChar, parentCode.Des);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertParentCode);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) parentCode.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.ParentCode parentCode)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@PCateCode", SqlDbType.NVarChar, parentCode.PCateCode);
			sql.AddParameter("@PName", SqlDbType.NVarChar, parentCode.PName);
			sql.AddParameter("@Des", SqlDbType.NVarChar, parentCode.Des);
			sql.AddParameter("@Id", SqlDbType.Int, parentCode.Id);
			sql.ExecuteSql(SqlUpdateParentCode);
		}

		public void Delete(Core.Business.ParentCode parentCode)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, parentCode.Id);
			sql.ExecuteSql(SqlDeleteParentCode);
		}

		public IList<Core.Business.ParentCode> GetAllParentCode()
		{
			IList<Core.Business.ParentCode> parentCodelist = new List<Core.Business.ParentCode>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllParentCode);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.ParentCode parentCode = new Core.Business.ParentCode();

					if (!reader.IsDBNull(0)) parentCode.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) parentCode.PCateCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) parentCode.PName = reader.GetString(2);
					if (!reader.IsDBNull(3)) parentCode.Des = reader.GetString(3);

					parentCode.MarkOld();
					parentCodelist.Add(parentCode);
				}
				reader.Close();
			}
			return parentCodelist;
		}
		#endregion
	}
}
